/**
 * Created by zdx  on 2016/8/26.
 *
 *
 */

define(['controller'], function (c) {
    var map =function () {
        this.map=null;

    };
    map.prototype.init=function (dom) {


        // 百度地图API功能
        this.map = new BMap.Map(dom,{enableMapClick:false});    // 创建Map实例
        this.map.centerAndZoom(new BMap.Point(104.2272944,30.5729027), 13);  // 初始化地图,设置中心点坐标和地图级别

        this.map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]}));
        this.map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    };
    map.prototype.addEventListener=function (events,fn) {
        this.map.addEventListener(events,fn);
    };
    map.prototype.createMark=function (op) {
        return new Marker(op);
    };
    map.prototype.createPolyline=function (path,op) {
        return new Polyline(path,op);
    };
    map.prototype.createContextMenu=function () {

        return new ContextMenu();
    };
    map.prototype.addContextMenu=function (menu) {
        this.map.addContextMenu(menu.mk);
    };

    map.prototype.addOverlay=function(Marker){
        this.map.addOverlay(Marker.mk)
    };

    function Marker (op) {//覆盖物对象
        this.mk=new BMap.Marker(new BMap.Point(op.lng, op.lat));
    }
    Marker.prototype.setIcon=function (url,size) {
        var size=new BMap.Size(size[0],size[1]);
        var myIcon = new BMap.Icon(url, size,{imageSize:size});
        this.mk.setIcon(myIcon);
    };

    Marker.prototype.setPosition=function (lnglat) {
        this.mk.setPosition(new BMap.Point(lnglat.lng,lnglat.lat));

    };
    Marker.prototype.setRotation=function (num) {
        this.mk.setRotation(num);

    };

    function Polyline(path,op) {//画线对象
        var newOP=$.extend({},op);
        this.mk=new BMap.Polyline(this.pathFormat(path),newOP);
    }

    Polyline.prototype.setPath=function (path) {
        this.mk.setPath(this.pathFormat(path));
    };
    Polyline.prototype.hide=function (path) {
        this.mk.hide();
    };
    Polyline.prototype.show=function (path) {
        this.mk.show();
    };

    Polyline.prototype.pathFormat=function (path) {
        var _path=[];
        $.each(path,function (i,point) {
            _path.push(new BMap.Point(point.lng, point.lat))
        });
        return _path;
    };

    function ContextMenu() {//右键菜单
        this.mk=new BMap.ContextMenu();
    }

    ContextMenu.prototype.addItems=function (items) {
        for(var i=0; i < items.length; i++){
            this.mk.addItem(new BMap.MenuItem(items[i].text,items[i].callback,100));
        }
    };

    function InfoWindow(content,opt) {
        this.mk=new BMap.InfoWindow(content,opt);
    }


   /* getWeather:function (lnglat,callback) {
        $.getJSON('http://api.map.baidu.com/telematics/v3/weather?location='+lnglat.lng+','+lnglat.lat+'&output=json&ak=j9rX4VOBPikrqZPRWh5Z3dEzPklXLzpD&callback=?'
            , function (json) {
                callback(json)
            });
    }*/

    return map;
});